package com.boc.langxiang.ui.adapter.me.section;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.boc.langxiang.App;
import com.boc.langxiang.Constant;
import com.boc.langxiang.R;
import com.boc.langxiang.contract.me.OrdersWaitReceiveFragmentContract;
import com.boc.langxiang.entity.events.OrdersEvent;
import com.boc.langxiang.entity.response.ProductEntity;
import com.boc.langxiang.entity.response.ProductListEntity;
import com.boc.langxiang.manager.PicassoImageLoader;
import com.boc.langxiang.ui.activity.me.OrdersApplyRefundActivity;
import com.boc.langxiang.util.StringUtil;
import com.chad.library.adapter.base.BaseSectionFootQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jakewharton.rxbinding.view.RxView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;
import java.util.concurrent.TimeUnit;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

public class OrdersWRSectionAdapter extends BaseSectionFootQuickAdapter<OrdersSection> {
    private OrdersWaitReceiveFragmentContract.View mView;

    private long lastTime = 0l;
    private long currentTime;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param context          The context.
     * @param layoutResId      The layout resource id of each item.
     * @param sectionHeadResId The section head layout id for each item
     * @param footResId
     * @param data             A new list is created out of this one to avoid mutable list
     */
    public OrdersWRSectionAdapter(Context context, int layoutResId, int sectionHeadResId, int footResId, List<OrdersSection> data, OrdersWaitReceiveFragmentContract.View view) {
        super(layoutResId, sectionHeadResId, footResId, data);
        this.mView = view;
    }

    @Override
    protected void convertHead(final BaseViewHolder helper, final OrdersSection item) {
        final ProductListEntity entity = item.getProductListEntity();
        helper.setText(R.id.tv_me_orders_numbers, StringUtil.isEmpty(entity.getOrderid()) ? "" : entity.getOrderid())
                .setText(R.id.tv_me_orders_status, "待收货")
                .setOnClickListener(R.id.linlayout_me_orders_head, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mView.startDetailActivity(entity.getId(), entity.getPrice());
                    }
                });
    }

    @Override
    protected void convertFoot(final BaseViewHolder helper, final OrdersSection item) {
        final ProductListEntity entity = item.getProductListEntity();
        helper.setText(R.id.tv_me_orders_count, "共" + entity.getProductnumber() + "件")
                .setText(R.id.tv_me_orders_totalprice, StringUtil.priceExecute(StringUtil.setPrice(entity.getPrice())))
                .setOnClickListener(R.id.linlayout_me_orders_bottom, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mView.startDetailActivity(entity.getId(), entity.getPrice());
                    }
                }).setOnClickListener(R.id.btn_me_orders_pay, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentTime = SystemClock.uptimeMillis();
                if (currentTime - lastTime > 1500l) {
                    View receiveView = View.inflate(mContext, R.layout.popwindow_orders_receive, null);
                    final PopupWindow receivePop = new PopupWindow(receiveView, (int) (App.getInstance().getResources().getDisplayMetrics().widthPixels * 0.8), LinearLayout.LayoutParams.WRAP_CONTENT);
                    receivePop.setAnimationStyle(R.style.me_pop_anim_center_style);
                    receivePop.setFocusable(true);
                    receivePop.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                    receivePop.setOutsideTouchable(true);
                    receivePop.setOnDismissListener(new PopupWindow.OnDismissListener() {
                        @Override
                        public void onDismiss() {
                            mView.setBackgroundAlpht(1f);
                        }
                    });
                    mView.showPopupWindow(receiveView, receivePop);
                    mView.setBackgroundAlpht(0.5f);
                    RxView.clicks(receivePop.getContentView().findViewById(R.id.atv_me_orders_rec_sure)).throttleFirst(2000, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new Action1<Void>() {
                                @Override
                                public void call(Void aVoid) {
                                    String id = item.getProductListEntity().getId();
                                    Log.d("orders-- footl:", id);
                                    mView.onReceiveSureClick(receivePop, id);
                                    mView.setBackgroundAlpht(1f);
                                    EventBus.getDefault().post(new OrdersEvent());
                                }
                            });
                    RxView.clicks(receivePop.getContentView().findViewById(R.id.atv_me_orders_rec_cel)).throttleFirst(2000, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new Action1<Void>() {
                                @Override
                                public void call(Void aVoid) {
                                    String id = item.getProductListEntity().getId();
                                    Log.d("orders-- footl:", id);
                                    mView.onReceiveGiveUpClick(receivePop);
                                    mView.setBackgroundAlpht(1f);
                                }
                            });
                    lastTime = currentTime;
                }
            }
        }).setOnClickListener(R.id.btn_me_orders_apply, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, OrdersApplyRefundActivity.class);
                intent.putExtra(Constant.ME_PRODUCT_TYPE_RETURN_PRICE, item.getProductListEntity().getPrice());
                intent.putExtra(Constant.ME_PRODUCT_TYPE_RETURN_ID, item.getProductListEntity().getOrderid());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    protected void convert(final BaseViewHolder helper, OrdersSection item) {
        final ProductEntity entity = item.t;
        helper.setText(R.id.tv_me_orders_content, StringUtil.isEmpty(entity.getTitle()) ? "" : entity.getTitle())
//                .setText(R.id.tv_me_orders_realsize, StringUtil.isEmpty(entity.getSize()) ? "" : entity.getSize())
                .setText(R.id.tv_me_orders_price, "¥" + (StringUtil.isEmpty(entity.getPrice()) ? "" : entity.getPrice()))
                .setText(R.id.tv_me_orders_num, "×" + (StringUtil.isEmpty(entity.getNumber()) ? "" : entity.getNumber()))
                .setOnClickListener(R.id.linlayout_me_orders_entity, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mView.startDetailActivity(entity.getId(), entity.getTotalPrice());
                    }
                });

        PicassoImageLoader.getInstance().displayUrlImage(mContext, entity.getPhotourl()
                , (ImageView) helper.getView(R.id.esv_me_orders_img));
    }
}
